#***********************************************************************************************************************
#*	filename: Makefile
#*	date: 2022-03-16 
#***********************************************************************************************************************

TARGET ?= dial_cm
SUFFIX ?= 

BUILD_DIR = build
DEST_DIR  = app_bin
INCLUDE_PATH = 
LISB = -lpthread 

CROSS_COMPILE = linux
ifeq ($(CROSS_COMPILE), arm) 
	CC = arm-oe-linux-gnueabi-gcc 
else
	CC = gcc
endif

OBJCOPY = $(CROSS_COMPILE)objcopy
SIZE = $(CROSS_COMPILE)size

#source file
SRC_DIR = .
SRC_DIR_FILE = main.c 
INCLUDE_PATH += -I ${SRC_DIR}
SOURCE_FILE += $(addprefix $(SRC_DIR)/, $(basename $(SRC_DIR_FILE)))

CORE_DIR = core
CORE_DIR_FILE = dbg.c cfg_info.c cJSON.c crc16.c tick.c file_io.c
INCLUDE_PATH += -I ${CORE_DIR}
SOURCE_FILE += $(addprefix $(CORE_DIR)/, $(basename $(CORE_DIR_FILE)))

NETWORK_DIR = network
NETWORK_DIR_FILE = tcpclient.c
INCLUDE_PATH += -I ${NETWORK_DIR}
SOURCE_FILE += $(addprefix $(NETWORK_DIR)/, $(basename $(NETWORK_DIR_FILE)))

SERIAL_DIR = serialport
SERIAL_DIR_FILE = serial_port.c
INCLUDE_PATH += -I ${SERIAL_DIR}
SOURCE_FILE += $(addprefix $(SERIAL_DIR)/, $(basename $(SERIAL_DIR_FILE)))

#add object file
OBJ_FILE = $(addsuffix .o, $(basename $(SOURCE_FILE)))
OBJ_FILE_PATH = $(addprefix ${BUILD_DIR}/, $(OBJ_FILE))

CFLAGS += -Wall -std=gnu99 -g ${INCLUDE_PATH} 
LDFLAGS = -lm ${LISB}

PACKAGE_BUILD_DATE ?= \"`date +%F\ %T`\"

.PHONY: size all clean

target: mkdir ${BUILD_DIR}/$(TARGET)$(SUFFIX)

all: clean target

mkdir:
	mkdir -p ${DEST_DIR}
	mkdir -p ${BUILD_DIR}/$(CORE_DIR)
	mkdir -p ${BUILD_DIR}/$(SERIAL_DIR)
	mkdir -p ${BUILD_DIR}/$(NETWORK_DIR)

${BUILD_DIR}/$(TARGET)$(SUFFIX): $(OBJ_FILE_PATH)
	$(CC) $(CFLAGS) $(OBJ_FILE_PATH) -o "$@" $(LISB)
	cp ${BUILD_DIR}/$(TARGET)$(SUFFIX) -arf ${DEST_DIR}

${BUILD_DIR}/%.o: %.c
	$(CC) -c -o "$@" $(CFLAGS) $<

clean:
	rm -fr ${BUILD_DIR}/*
	rm -fr ${DEST_DIR}/*
	
install:
	sudo sshpass -p 'csg2020' scp ${BUILD_DIR}/$(TARGET)$(SUFFIX) root@192.168.2.219:/root


#***********************************************************************************************************************
#*	endline
#***********************************************************************************************************************
